// const gulp = require('gulp')
require('babel-polyfill')
const {  task, src, dest, series, watch } = require('gulp')
const concat = require('gulp-concat')
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const less = require('gulp-less')
const cssClean = require('gulp-clean-css')
const htmlmin = require('gulp-htmlmin')
const livereload = require('gulp-livereload')
const connect = require('gulp-connect')
const open = require('open')

const rev = require('gulp-rev')
const revCollector = require('gulp-rev-collector')
const del = require('del')
const babel = require('gulp-babel')

const cheerio = require('gulp-cheerio')
// 注册任务
// gulp.task('任务名',function () {

// })
// 处理js文件
task('js',series(()=>{
  return src('src/js/**/*.js')
    .pipe(babel({
      presets:['es2015']
    }))
    .pipe(concat('build.js'))
    .pipe(rev())
    .pipe(dest('dist/js/'))
    .pipe(uglify())
    .pipe(livereload())
    .pipe(connect.reload())
    .pipe(rev.manifest())
    .pipe(dest('rev/js'))
}))

// 处理less文件
task('less',series(()=>{
  return src('src/less/**/*.less')
  .pipe(less())
  .pipe(dest('src/css/'))
  .pipe(livereload())
  .pipe(connect.reload())
}))

// 处理css文件
task('css',series(()=>{
  return src('src/css/**/*.css')
    .pipe(concat('build.css'))
    // .pipe(rename({suffix:'.min'}))
    .pipe(rev())
    .pipe(cssClean({compatibility: 'ie8'}))
    .pipe(dest('dist/css/'))
    .pipe(livereload())
    .pipe(connect.reload())
    .pipe(rev.manifest())
    .pipe(dest('rev/css'))
}))

// task('inject',series(()=>{
//   return src('index.html')
//   .pipe(inject('https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'))
  
// }))
// 动态的在head标签开始注入第三方js脚本 然后输出到dist/index.html 等下面处理html的时候则直接去dist/index.html中取数据流
// 插件网址 https://www.npmjs.com/package/gulp-cheerio
task('cheerio',series(()=>{
  return src('index.html')
      .pipe(cheerio(($,file)=>{
        $('head').each(function() {
          const head = $(this)
          let headText = head.html()
          headText = `<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'></script>` + headText
          console.log('headText----',headText)
          head.html(headText);
        });
      }))
      .pipe(dest('dist/'))
}))
task('html',series(()=>{
  return src(['dist/index.html','rev/**/*.json'])
    .pipe(revCollector())
    .pipe(htmlmin({collapseWhitespace:true}))
    .pipe(dest('dist/'))
    .pipe(livereload())
    .pipe(connect.reload())
}))

task('watch',()=>{
  livereload.listen()
  watch('src/js/**/*.js',series(['js']))
  watch(['src/css/**/*.css', 'src/less/**/*.less'], series(['css']))
})

task('server',()=>{
  connect.server({
    root:'dist/',
    host:'10.12.0.155',
    port: '9527',
    livereload: true
  })
  open('http://10.12.0.155:9527')
  watch('src/js/**/*.js',series(['js']))
  watch(['src/css/**/*.css', 'src/less/**/*.less'], series(['css']))

})

task('clean',series(()=>{
  return del(['dist'],{'force': true})
}))


task('default',series('clean','js','less','css','cheerio','html'))